/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import EventBus from "./eventBus";
let TAG = "eventBus_test ";

function dispatch_test() {
  let basicEventBusCallBack = (event) => {
    // print(TAG + " basicEventBus message received " + event.type)
  }
  EventBus.addEventListener("basicEventBus", basicEventBusCallBack,this)
  let start
  let end
  start = Date.now()
  for (let index = 0; index < 10000; index++) {
    EventBus.dispatch("basicEventBus",basicEventBusCallBack)
  }
  end = Date.now()
  print(`eventBus_dispatch: ${end - start} ms`)
}

function removeEventListener_test() {
  let basicTestCallBack = (event) => {
    print(TAG + " basicTest message received " + event.type)
  }
  EventBus.addEventListener("basicTest", basicTestCallBack,this)
  let start
  let end
    start = Date.now()
    for (let index = 0; index < 50000; index++) {
      EventBus.removeEventListener("basicTest", basicTestCallBack,this)
    }
    end = Date.now()

    let prt = EventBus.hasEventListener('basicTest', basicTestCallBack,this)
    if (!prt) {
      print(`eventBus_removeEventListener: ${end - start} ms`)
    } else {
      throw new Error('eventBus_removeEventListener err')
    }
}

function ListeningWithDeletion_test(){
  let basicTestCallBack = (event) => {
    print(TAG + " basicTest message received " + event.type)
  }
  EventBus.addEventListener("basicTest", basicTestCallBack,this)
  let start
  let end
    start = Date.now()
    for (let index = 0; index < 50000; index++) {
      EventBus.addEventListener("basicTest", basicTestCallBack,this)
      EventBus.removeEventListener("basicTest", basicTestCallBack,this)
    }
    end = Date.now()
    let prt = EventBus.hasEventListener('basicTest', basicTestCallBack,this)
    if (!prt) {
      print(`eventBus_addEventListener+removeEventListener: ${end - start} ms`)
    } else {
      throw new Error('eventBus_addEventListener+removeEventListener err')
    }
}

function addEventListener_test() {
  let basicTestCallBack = (event) => {
    print(TAG + " basicTest message received " + event.type)
  }
  let start
  let end
    start = Date.now()
    //Circular subscription here leads to message stacking, so it takes a long time
    for (let index = 0; index <50000; index++) {
      EventBus.addEventListener(`basicTest`, basicTestCallBack,this)
    }
    end = Date.now()
    let prt = EventBus.hasEventListener('basicTest', basicTestCallBack,this)
    EventBus.removeEventListener("basicTest", basicTestCallBack,this)
    if (prt) {
      print(`eventBus_addEventListener: ${end - start} ms`)
    } else {
      throw new Error('eventBus_addEventListener err')
    }
}

function getEvents_test() {
  let abcCallBack = (event) => {
  }
  let cbdCallBack = (event) => {
  }
  EventBus.addEventListener("abc", abcCallBack)
  EventBus.addEventListener("cbd", cbdCallBack)
  let start
  let end
  let str
  start = Date.now()
  for (let index = 0; index < 10000; index++) {
    str = EventBus.getEvents()
  }
  end = Date.now();
  if (str) {
    print(`eventBus_getEvents: ${end - start} ms`)
  } else {
    throw new Error('eventBus_getEvents err')
  }
  // print(EventBus.getEvents())

}

function hasEventListener_test() {
  let basicTestCallBack = (event) => {
    print(TAG + " basicTest message received " + event.type)
  }
  let isHasSecond = false;
  let start
  let end
    EventBus.addEventListener("basicEventBus", basicTestCallBack,this)
    start = Date.now()
    for (let index = 0; index < 50000; index++) {
      isHasSecond = EventBus.hasEventListener("basicEventBus", basicTestCallBack,this)
    }
    end = Date.now()
  if (isHasSecond) {
    print(`eventBus_hasEventListener: ${end - start} ms`)
  } else {
    throw new Error('eventBus_hasEventListener err')
  }
}


let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
  EventBus.clear()
  dispatch_test()
  removeEventListener_test()
  ListeningWithDeletion_test()
  addEventListener_test()
  getEvents_test()
  hasEventListener_test()
}

ArkTools.waitAllJitCompileFinish();

EventBus.clear()
dispatch_test()
removeEventListener_test()
ListeningWithDeletion_test()
addEventListener_test()
getEvents_test()
hasEventListener_test()


